using System;
using TMPro;
using UnityEngine;
using UnityEngine.Serialization;

namespace Voodoo.Distribution
{
	[Serializable]
	public class Translator : MonoBehaviour
	{
		[SerializeField]
		private TMP_Text _textMesh;

		[SerializeField]
		private string _defaultValue;

		[FormerlySerializedAs("AtStart")]
		[SerializeField]
		private bool _atStart = true;

		[FormerlySerializedAs("AtEnable")]
		[SerializeField]
		private bool _onEnable;

		public string key;

		private TMP_Text TextMesh => _textMesh ?? (_textMesh = GetComponent<TMP_Text>());

		private void Start()
		{
			if (_atStart)
			{
				Translate();
			}
		}

		private void OnEnable()
		{
			if (_onEnable)
			{
				Translate();
			}
		}

		public virtual void Translate(string language = null)
		{
			TextMesh.text = Localization.GetTranslation(key, language);
		}

		private void UpdateRightToLeft()
		{
			if (Localization.IsRightToLeftLanguage)
			{
				TextMesh.alignment = TextAlignmentOptions.Right;
				TextMesh.isRightToLeftText = true;
			}
			else
			{
				TextMesh.alignment = TextAlignmentOptions.Left;
				TextMesh.isRightToLeftText = false;
			}
		}
	}
}
